<?php
namespace dl\Service;

use Co;
use Swoole\Coroutine\MySQL;
use Swoole\Coroutine\Redis;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;

class Swoole {

    private Server $ws;
    private Redis $redis;
    private Mysql $mysql;

    public function __construct() {
        Co::set([
            'enable_preemptive_scheduler' => 0
        ]);

        $this->ws = new Server('0.0.0.0', 9502, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

        $this->ws->set([
            'open_http_protocol' => true //开启http协议处理
        ]);

        $this->ws->onWorkerStart([$this, 'onWorkerStart']);
        $this->ws->onOpen([$this, 'onOpen']);
        $this->ws->onMessage([$this, 'onMessage']);
        $this->ws->onRequest([$this, 'onRequest']);
        $this->ws->onClose([$this, 'onClose']);

        //开启tcp的监听端口
        $serv = $this->ws->addlistener('0.0.0.0', 9504, SWOOLE_SOCK_TCP);
        $serv->set([]);
        $serv->on('receive', [$this, 'onReceive']);
    }

    /**
     *  Worker进程/Task进程启动时触发；这里创建的对象可以在进程生命周期内使用
     */
    public function onWorkerStart() {
        //连接redis
        $this->redis = new Redis();
        $this->redis->connect('myredis', 6379, 1.5);
        //$this->redis->auth(''); //密码

        $this->redis->del('toupee_cus_fd'); //在线的客户
        $this->redis->del('toupee_cus_msg'); //客户最近

        //连接mysql
        $this->mysql = new MySQL();
        $this->mysql->connect([
            'host'        => 'mysql8',
            'user'        => 'root',
            'password'    => 'qingdao2021',
            'database'    => 'db_webchat',
            'port'        => 3306,
            'timeout'     => 1.5,
            'charset'     => 'UTF-8',
            'strict_type' => true, //开启严格模式，返回强类型
            'fetch_mode'  => true //开启fetch模式，可与pdo一样使用fetch/fetchAll逐行或获取全部结果集
        ]);
        
    }

    /**
     *  打开websocket连接
     *
     * @param Server $server
     * @param Request $request
     */
    public function onOpen(Server $server, Request $request) {
        $server->push($request->fd, 'hello,welcome\n');
    }

    /**
     * websocket 接受消息
     * @param Server $server
     * @param Frame $frame
     */
    public function onMessage(Server $server, Frame $frame) {

    }

    /**
     * http请求调用
     * @param Request $request
     * @param Response $response
     */
    public function onRequest(Request $request, Response $response) {

        if ($request->server['path_info'] == '/favicon.ico'
            || $request->server['request_uri'] == '/favicon.ico') {
            $response->end();
            return;
        }
        $controller = "";
        $action = "";
        $requestPath = trim($request->server['request_uri'], '/');
        if ($requestPath != "") {
            list($controller, $action) = explode('/', trim($request->server['request_uri'], '/'));
        }
        if (!$controller) {
            $controller = "index";
        }
        if (!$action) {
            $action = "index";
        }
        $controller = "app\\controller\\".ucfirst($controller);
        (new $controller)->$action($request, $response);

    }


    /**
     * 关闭连接时调用
     *
     * @param Server $server
     * @param int $fd
     * @param int $reactorId
     */
    public function onClose(Server $server, int $fd, int $reactorId) {

    }

    /**
     * TCP接受数据时调用
     *
     * @param Server $server
     * @param int $fd
     * @param int $reactorId
     * @param string $data
     */
    public function onReceive(Server $server, int $fd, int $reactorId, string $data) {

    }


}